package com.example.vault;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.vault.core.VaultTemplate;
import org.springframework.vault.support.VaultResponse;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/vault")
public class VaultController {

    @Autowired
    private VaultTemplate vaultTemplate;

    @GetMapping("/paths")
    public ResponseEntity<Map<String, Object>> listPaths() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // KV v2 引擎需要使用 metadata 路径来列出子路径
            List<String> paths = vaultTemplate.list("kv/metadata");
            
            result.put("status", "success");
            result.put("message", "成功获取 Vault 路径列表");
            result.put("paths", paths);
            result.put("count", paths != null ? paths.size() : 0);
            result.put("timestamp", System.currentTimeMillis());
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("status", "error");
            result.put("message", "获取 Vault 路径失败: " + e.getMessage());
            result.put("error", e.getClass().getSimpleName());
            result.put("timestamp", System.currentTimeMillis());
            return ResponseEntity.status(500).body(result);
        }
    }

    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getVaultStatus() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 尝试读取一个已知路径来测试 Vault 状态 (KV v2 引擎使用 data 路径)
            VaultResponse response = vaultTemplate.read("kv/data/application");
            
            result.put("status", "success");
            result.put("message", "Vault 服务正常");
            result.put("connected", true);
            result.put("hasData", response != null && response.getData() != null);
            result.put("timestamp", System.currentTimeMillis());
            
            if (response != null && response.getData() != null) {
                result.put("dataKeys", response.getData().keySet());
            }
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("status", "error");
            result.put("message", "Vault 连接失败: " + e.getMessage());
            result.put("connected", false);
            result.put("error", e.getClass().getSimpleName());
            result.put("timestamp", System.currentTimeMillis());
            return ResponseEntity.status(500).body(result);
        }
    }

    @PostMapping("/write")
    public ResponseEntity<Map<String, Object>> writeToVault(@RequestBody Map<String, Object> data) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 写入测试数据到 kv/data/test 路径 (KV v2 引擎)
            String path = "kv/data/test";
            vaultTemplate.write(path, data);
            
            result.put("status", "success");
            result.put("message", "成功写入数据到 Vault");
            result.put("path", path);
            result.put("dataKeys", data.keySet());
            result.put("timestamp", System.currentTimeMillis());
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("status", "error");
            result.put("message", "写入 Vault 失败: " + e.getMessage());
            result.put("error", e.getClass().getSimpleName());
            result.put("timestamp", System.currentTimeMillis());
            return ResponseEntity.status(500).body(result);
        }
    }

    @GetMapping("/read/{path}")
    public ResponseEntity<Map<String, Object>> readFromVault(@PathVariable String path) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // KV v2 引擎需要使用 data 路径前缀
            VaultResponse response = vaultTemplate.read("kv/data/" + path);
            
            result.put("status", "success");
            result.put("message", "成功从 Vault 读取数据");
            result.put("path", "kv/data/" + path);
            result.put("data", response != null ? response.getData() : null);
            result.put("timestamp", System.currentTimeMillis());
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("status", "error");
            result.put("message", "从 Vault 读取数据失败: " + e.getMessage());
            result.put("error", e.getClass().getSimpleName());
            result.put("timestamp", System.currentTimeMillis());
            return ResponseEntity.status(500).body(result);
        }
    }
}